﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Zero.POCO;
using Zero.POCO.Helper;

namespace DevExpress.MailClient.Win.Forms.Campagne
{
    public partial class frmEditMarque : XtraForm
    {
        private bool _isMarqueModified;
        public Marque marque { get; set; }
        private bool _newMarque = true;
        public Guid MarqueId { get; set; }


        public frmEditMarque(Marque marquehist, bool newMarque, List<Annonceur> annonceurs, bool FromAnnonceur = false)
        {
            InitializeComponent();
            DialogResult = DialogResult.Cancel;
            _newMarque = newMarque;
            if (_newMarque)
            {
                MarqueId = default(Guid);
                CBEDomaine.SelectedIndex = 5;
                _isMarqueModified = true;
                if (FromAnnonceur)
                    SLUAnnonceur.Enabled = false;
                else
                {

                    SLUAnnonceur.Properties.DataSource = annonceurs;
                    /*SLUAnnonceur.Properties.DisplayMember = "Name";
                    SLUAnnonceur.Properties.ValueMember = "id";
                    SLUAnnonceur.Properties.PopulateColumns();
                    SLUAnnonceur.Properties.Columns[2].Visible = false;*/
                }
            }
            else
            {MarqueId = marquehist.id;
                SLUAnnonceur.SelectedText = marquehist.Type.ToString();
                _isMarqueModified = false;
            }
            }

        DialogResult QueryDelete()
        {
            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.DeleteQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    DeleteRecord();
                    return DialogResult.Yes;
            }
        }

        private void DeleteRecord()
        {
            throw new NotImplementedException();
        }

        DialogResult QueryClose()
        {
            if (!_isMarqueModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    ApplyChanges();
                    return DialogResult.Yes;
            }
        }

        private void ApplyChanges()
        {
            throw new NotImplementedException();
        }


        private void TENom_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (TENom.Text.Trim().Length <= 0)
                e.Cancel = true;
        }

        private void TENom_TextChanged(object sender, EventArgs e)
        {
            _isMarqueModified = true;
        }

        private void CBEDomaine_TextChanged(object sender, EventArgs e)
        {
            _isMarqueModified = true;
        }

        private void TempMarque_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = QueryClose();
            e.Cancel = result == DialogResult.Cancel;
        }

        private void SLUAnnonceur_TextChanged(object sender, EventArgs e)
        {
            _isMarqueModified = true;
        }

        private void BtnSauver_Click(object sender, EventArgs e)
        {
            _isMarqueModified = false;
            DialogResult result = QueryClose();
            marque = AnnonceurFactory.CreateMarque(TENom.Text, CBEDomaine.SelectedText.ToMarque());
            //_editionAnnonceurPresenter.WriteChanges();
            if (result == DialogResult.Yes) this.Close();
        }

        private void BtnAnnuler_Click(object sender, EventArgs e){
            DialogResult result = QueryClose();
            if (result == DialogResult.Yes) this.Close();
        }

        private void SLUAnnonceur_Popup(object sender, EventArgs e)
        {}

        private void SLUAnnonceur_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (SLUAnnonceur.Enabled)
            {
                if(SLUAnnonceur.ItemIndex==0)
                    e.Cancel = true;
            }
        }
    }
}